package com.hctforgreen.greenservice.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	/**
	 * 
	 * 
	 * @param name
	 *            名字
	 * @return 名字是否合法 不能包含数字和特殊字符 只能为汉字或字母
	 */
	public static boolean isNameLegal(String name) {
		if (name == null || "".equals(name.trim())) {
			return false;
		}
		char[] charArr = name.toCharArray();
		for (int i = 0; i < charArr.length; i++) {
			if (charArr[i] >= '0' && charArr[i] <= '9') {// 是否为数字
				return false;
			}
		}
		String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？_]";
		Pattern pat = Pattern.compile(regEx);
		for (int i = 0; i < name.length(); i++) {
			if (pat.matcher(String.valueOf(name.charAt(i))).matches()) {// 是否为特殊字符
				return false;
			}
		}

		return true;
	}
	
	/**
	 * 
	 * 2016.5.27---390694添加
	 * @param 
	 * @return 条码不能包含特殊字符 只能为数字和字母
	 */
	public static boolean isBarcodeLegal(String barcode) {
		
		String regEx = "[`~!@#$%^&*()+=|{}':;'\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？_]";
		Pattern pat = Pattern.compile(regEx);
		for (int i = 0; i < barcode.length(); i++) {
			if (pat.matcher(String.valueOf(barcode.charAt(i))).matches()) {// 是否为特殊字符
				return true;
			}
		}

		return false;
	}
	

	/**
	 * 
	 * @param phoneNum
	 *            电话号码或座机号码
	 * @return 是否为电话号码或座机号码
	 */
	public static boolean isPhone(String phoneNum) {
		if (phoneNum == null || "".equals(phoneNum.trim())) {
			return false;
		}
		if (checkPhoneNr(phoneNum) || checkPhoneNrWithoutCode(phoneNum)
				|| checkPhoneNrWithoutLine(phoneNum) || isMobileNO(phoneNum)) {
			return true;
		}

		return false;

	}

	static public boolean checkPhoneNr(String phoneNr) {
		String regex = "^[0]\\d{2,3}\\-\\d{7,8}";

		return startCheck(regex, phoneNr);
	}

	static public boolean checkPhoneNrWithoutCode(String phoneNr) {
		String reg = "^[1-9]\\d{6,7}";

		return startCheck(reg, phoneNr);
	}

	static public boolean checkPhoneNrWithoutLine(String phoneNr) {
		String reg = "^[0]\\d{10,11}";

		return startCheck(reg, phoneNr);
	}

	static public boolean startCheck(String reg, String string) {
		boolean tem = false;

		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(string);
		tem = matcher.matches();
		return tem;
	}

	public static boolean isMobileNO(String mobiles) {

		return mobiles.length() == 11;
	}
}
